package com.bw.redis.config;

import com.bw.redis.domain.DictionaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Component
public class DictionaryConstant {
    @Autowired
    private RedisTemplate redisTemplate;
    //redis的字典数据常量
    public static final String DICTIONARY_KEY = "dictionary";
    //性别字段
    public static final String DICTIONARY_SEX = "sexs:sex";

    public static final String DICTIONARY_ROLE = "role:role";

    /**
     * 缓存中查数据字典
     * @param key redis key
     * @return
     */
    public List<DictionaryVo> getDataDictionary(String key) {
        if (redisTemplate.hasKey("dictionary")) {
            Map<Object, Object> entries = redisTemplate.opsForHash().entries(DICTIONARY_KEY);
            Object o = entries.get(key);
            if (o instanceof List){
                List<DictionaryVo> list = (List<DictionaryVo>) entries.get(key);
                return list;
            }
        }
        return null;
    }
}
